﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DDD;
using Xunit;

namespace DokiDokiTest {
    public class TestMesh {
        [Fact]
        public void Test_New1 () {
            var msh = new Mesh ();

            Assert.Equal (1, msh.SubmeshCount);
            Assert.Equal (null, msh.VertexBuffer);
            Assert.Equal (null, msh.IndexBuffer);
            Assert.Equal (null, msh.Appearance);
        }

        [Fact]
        public void Test_New2 () {
            var msh = new Mesh (2);

            Assert.Equal (2, msh.SubmeshCount);
            Assert.Equal (null, msh.GetVertexBuffer (0));
            Assert.Equal (null, msh.GetVertexBuffer (1));
            Assert.Equal (null, msh.GetIndexBuffer (0));
            Assert.Equal (null, msh.GetIndexBuffer (1));
            Assert.Equal (null, msh.GetAppearance (0));
            Assert.Equal (null, msh.GetAppearance (1));
        }

        [Fact]
        public void Test_Enumerator () {
            var msh = new Mesh (2);

            var vsum = msh.VertexBuffers.Sum (x => 1);
            var isum = msh.IndexBuffers.Sum (x => 1);
            var asum = msh.Appearances.Sum (x => 1);

            Assert.Equal (2, msh.SubmeshCount);
            Assert.Equal (0, vsum);
            Assert.Equal (0, isum);
            Assert.Equal (0, asum);
        }

        [Fact]
        public void Test_SetVertexBuffer () {
            var msh = new Mesh (2);
            var vbuf1 = new VertexBuffer ();
            var vbuf2 = new VertexBuffer ();

            msh.SetVertexBuffer (0, vbuf1);
            msh.SetVertexBuffer (1, vbuf2);

            Assert.Equal (vbuf1, msh.GetVertexBuffer (0));
            Assert.Equal (vbuf2, msh.GetVertexBuffer (1));
        }

        [Fact]
        public void Test_SetIndexBuffer () {
            var msh = new Mesh (2);
            var ibuf1 = new IndexBuffer<int> (PrimitiveType.Triangle, 10);
            var ibuf2 = new IndexBuffer<int> (PrimitiveType.Triangle, 10);

            msh.SetIndexBuffer (0, ibuf1);
            msh.SetIndexBuffer (1, ibuf2);

            Assert.Equal (ibuf1, msh.GetIndexBuffer (0));
            Assert.Equal (ibuf2, msh.GetIndexBuffer (1));
        }

        [Fact]
        public void Test_SetAppearance () {
            var msh = new Mesh (2);
            var app1 = new Appearance ();
            var app2 = new Appearance ();

            msh.SetAppearance (0, app1);
            msh.SetAppearance (1, app2);

            Assert.Equal (app1, msh.GetAppearance (0));
            Assert.Equal (app2, msh.GetAppearance (1));
        }

        [Fact]
        public void Test_References () {
            var msh = new Mesh (2);
            var vbuf1 = new VertexBuffer ();
            var vbuf2 = new VertexBuffer ();
            var ibuf1 = new IndexBuffer<int> (PrimitiveType.Triangle, 10);
            var ibuf2 = new IndexBuffer<int> (PrimitiveType.Triangle, 11);
            var app1 = new Appearance ();
            var app2 = new Appearance ();
            msh.SetVertexBuffer (0, vbuf1);
            msh.SetVertexBuffer (1, vbuf2);
            msh.SetIndexBuffer (0, ibuf1);
            msh.SetIndexBuffer (1, ibuf2);
            msh.SetAppearance (0, app1);
            msh.SetAppearance (1, app2);

            var objs = msh.References;

            Assert.Equal (6, objs.Count ());
            Assert.Equal (true, objs.All (x => x != null));
        }

        [Fact]
        public void Test_Find () {
            var vbuf1 = new VertexBuffer ();
            var vbuf2 = new VertexBuffer ();
            var ibuf1 = new IndexBuffer<int> (PrimitiveType.Triangle, 10);
            var ibuf2 = new IndexBuffer<int> (PrimitiveType.Triangle, 11);
            var app1 = new Appearance ();
            var app2 = new Appearance ();
            var msh = new Mesh (2);
            msh.SetVertexBuffer (0, vbuf1);
            msh.SetVertexBuffer (1, vbuf2);
            msh.SetIndexBuffer (0, ibuf1);
            msh.SetIndexBuffer (1, ibuf2);
            msh.SetAppearance (0, app1);
            msh.SetAppearance (1, app2);

            vbuf1.UserID = 101;
            vbuf2.UserID = 102;
            ibuf1.UserID = 103;
            ibuf2.UserID = 104;
            app1.UserID = 105;
            app2.UserID = 106;
            msh.UserID = 107;

            Assert.Equal (vbuf1, msh.Find(101));
            Assert.Equal (vbuf2, msh.Find (102));
            Assert.Equal (ibuf1, msh.Find (103));
            Assert.Equal (ibuf2, msh.Find (104));
            Assert.Equal (app1, msh.Find (105));
            Assert.Equal (app2, msh.Find (106));
            Assert.Equal (msh, msh.Find (107));
        }
    }
}
